//============================================================================
// Name        : ms013.cpp
// Author      : Tadas
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cmath>
#include <ctime>

#include "lib.h"

using namespace std;

double func(double x, double y, double z)
{
	double temp, ats;

	temp = x*x+y*y;
	temp = sqrt(temp);
	temp -= 3;
	temp *= temp;

	ats = z*z + temp;
	return ats;
}

double tank(double z)
{
	double ats, temp;

	temp = 5 * z;
	ats = exp(temp);
	return ats;
}

int main()
{
	seed = time(NULL);
	int j, ij=12;
	long i, n1 = 1000, n;
	double x, y, z;
	double sw, swx, swy, swz;
	double varw, varx, vary, varz;
	double w, dw, xw, dx, yw, dy, zw, dz;
	double den, vol, a, den2, volume;
	double v, vx,vy,vz, s;
	a = sqrt(2);
	den=1;
	den2= den*den;

	v=vx=vy=vz=volume=0;
	sw=swx=swy=swz=0.0;
	varw=varx=vary=varz=0.0;
	vol = 48;
	n=0;

	printf("Mase\tmc x\tmc y\tmc z\n");

	for (j=0; j<ij; j++)
	{
		for (i=0; i<n1; i++)
		{
			n++;
			x=3*randGe()+1;
			y=7*randGe()-3;
			z=4*randGe()-2;
			if (func(x, y, z)<=1)

			{
				sw  += den;
				swx += x*den;
				swy += y*den;
				swz += z*den;

				varw += den2;
				varx += x*x*den2;
				vary += y*y*den2;
				varz += z*z*den2;
			}
		}
		n1 *= 2;

		w  = sw /n*vol;
		xw = swx/sw;
		yw = swy/sw;
		zw = swz/sw;

		v+=w;
		vx+=xw;
		vy+=yw;
		vz+=zw;

		volume += w/den;

		//printf("%.3f\t(%.3f ; %.3f ; %.3f)\n", w, xw, yw, zw);
	}

	cout << "Mases c. x: " << vx << " y: " << vy << " z: " << vz << endl;
		//	printf("Turis: %.3f\n", volume/12);
		cout << "Turis: " << volume << endl;


	n1 = 1000;
	v=vx=vy=vz=volume=0;
	sw=swx=swy=swz=0.0;
	varw=varx=vary=varz=0.0;
	a = exp(5);
	vol = 3*7*fabs(0.2-0.2*a);
	n=0;
	printf("Mase\t\tmc x\tmc y\tmc z\n");
	for (j=0; j<ij; j++)
	{
		for (i=0; i<n1; i++)
		{
			n++;
			x=3*randGe()+1;
			y=7*randGe()-3;
			s=(0.2*a-0.2)*randGe()+0.2;
			z=log(5*s)/5;
			den = tank(z);
			den2 = den*den;
			if (func(x, y, z)<=1)
			{
				sw  += den;
				swx += x*den;
				swy += y*den;
				swz += z*den;

				varw += den2;
				varx += x*x*den2;
				vary += y*y*den2;
				varz += z*z*den2;
			}
		}
		n1 *= 2;

		w  = sw /n*vol;
		xw = swx/sw;
		yw = swy/sw;
		zw = swz/sw;

		volume += w/den;

		v+=w;
		vx+=xw;
		vy+=yw;
		vz+=zw;

		//printf("%.3f\t(%.3f ; %.3f ; %.3f)\n", w, xw, yw, zw);
	}


	printf("\nVidurkis:\n%.3f\t(%.3f ; %.3f ; %.3f)\n", v/12, vx/12, vy/12, vz/12);
	printf("Turis: %.3f\n", volume/12);
	

	return 0;
}
